package com.example.daoru;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.alibaba.fastjson.JSONObject;
import com.example.daoru.pojo.Xhh;
import com.example.daoru.utils.MultipartFileToFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @auther: Wyz
 * @date: 2021/12/5 00:03
 * @return
 */
@RestController
@Slf4j
public class DaoruController {

    @PostMapping(value = "/test", headers = "content-type=multipart/form-data", consumes = "multipart/*")
    public void readExcel1(@RequestParam("filePath") MultipartFile filePath) {
        File file = null;
        try {
            file = MultipartFileToFile.multipartFileToFile(filePath);
        } catch (IOException e) {
            log.info("解析流异常，异常信息为：{}", e);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //导入的标题和说明的设置
        ImportParams params = new ImportParams();
        //设置说明
        params.setHeadRows(3);
        params.setStartRows(3);

        //导入获取集合

        List<SealAllVo> sealCfBos = ExcelImportUtil.importExcel(file, SealAllVo.class, params);

    }

    @PostMapping(value = "/test2", headers = "content-type=multipart/form-data", consumes = "multipart/*")
    public void readExcel(@RequestParam("filePath") MultipartFile multipartFile, HttpServletResponse response) {
        try {
            ImportParams params = new ImportParams();
            params.setHeadRows(3);
            params.setStartRows(3);
            InputStream inputStream = multipartFile.getInputStream();
            List<SealAllVo> sealCfBos = ExcelImportUtil.importExcel(inputStream, SealAllVo.class, params);
        } catch (Exception e) {
            log.info("文件读取失败");
        }
    }

    @GetMapping("123")
    public void exportExcel(HttpServletResponse response) throws InterruptedException, IOException {
        response.reset(); // 清除buffer缓存
        // 指定下载的文件名
        response.setHeader("Content-Disposition", "attachment;filename=contacts" + System.currentTimeMillis() + ".xlsx");
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        ServletOutputStream outputStream = response.getOutputStream();
//        List<SealAllVo> sealCfBos = new ArrayList<>();
//        SealAllVo sealAllVo = new SealAllVo("1","2","3","4","5","6","7","8","9","0","01","02","03");
//        sealCfBos.add(sealAllVo);
        String s= "[\n" +
                "    {\n" +
                "        \"id\": \"AXXA07MHfUST3rXP4PO2\",\n" +
                "        \"deleted\": false,\n" +
                "        \"createTime\": 1605257407447,\n" +
                "        \"updateTime\": 1605257407447,\n" +
                "        \"master\": true,\n" +
                "        \"roomType\": 2,\n" +
                "        \"slaveLevel\": 1,\n" +
                "        \"name\": \"l-xbk-salary-mysql1.syc.prod.ali.qr\",\n" +
                "        \"desc\": \"无\",\n" +
                "        \"version\": 5,\n" +
                "        \"current\": true,\n" +
                "        \"host\": \"10.102.2.208\",\n" +
                "        \"port\": 3306,\n" +
                "        \"instanceInfo\": {\n" +
                "            \"type\": \"\",\n" +
                "            \"cpu\": 2,\n" +
                "            \"memory\": \"\",\n" +
                "            \"store\": \"\"\n" +
                "        },\n" +
                "        \"mark\": \"时间：2020-11-13 16:50:07 \\n\\r 操作人: 仇梓旭 \\n\\r 操作动作：(主从切换)修改实例, 实例名称：L-XBK-SALARY-MYSQL1.SYC.PROD.ALI.QR 版本：5\",\n" +
                "        \"operatorName\": \"仇梓旭\",\n" +
                "        \"operatorId\": null\n" +
                "    },\n" +
                "    {\n" +
                "        \"id\": \"AWcwXjrjUWHIiZO6rmEf\",\n" +
                "        \"deleted\": false,\n" +
                "        \"createTime\": 1542704741123,\n" +
                "        \"updateTime\": 1594306721958,\n" +
                "        \"master\": false,\n" +
                "        \"roomType\": 2,\n" +
                "        \"slaveLevel\": 1,\n" +
                "        \"name\": \"l-xbk-salary-mysql0.syc.prod.ali.qr\",\n" +
                "        \"desc\": \"无\",\n" +
                "        \"version\": 1,\n" +
                "        \"current\": true,\n" +
                "        \"host\": \"10.102.12.96\",\n" +
                "        \"port\": 3306,\n" +
                "        \"instanceInfo\": {\n" +
                "            \"type\": \"\",\n" +
                "            \"cpu\": 2,\n" +
                "            \"memory\": \"\",\n" +
                "            \"store\": \"\"\n" +
                "        },\n" +
                "        \"mark\": \"时间：2018-11-20 17:05:41 \\n\\r 操作人:  \\n\\r 操作动作：新增实例,实例名称:L-XBK-SALARY-MYSQL0.SYC.PROD.ALI.QR\",\n" +
                "        \"operatorName\": \"\",\n" +
                "        \"operatorId\": \"\"\n" +
                "    }\n" +
                "]";
        List<Xhh> xhhs = JSONObject.parseArray(s, Xhh.class);
        ExportParams test = new ExportParams("2412312", "测试");
        test.setType(ExcelType.XSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(test,
                Xhh.class, xhhs);
        workbook.write(outputStream);
        outputStream.close();
    }

}
